package com.zzgele.qmzb.api.utils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Martin on 2017-07-15.
 */

/**
 * 类说明:提取身份证相关信息
 */
public class IdCardInfoUtil {
    // 省份
    private String province;
    // 城市
    private String city;
    // 区县
    private String region;
    // 省份
    private int provinceId;
    // 省份
    private int cityId;
    // 区县
    private int regionId;
    // 年份
    private int year;
    // 月份
    private int month;
    // 日期
    private int day;
    // 性别 [1=男/2=女]
    private int genderId;
    // 性别字符
    private String gender;
    // 出生日期
    private String birth_day;

    private Map<String, String> cityCodeMap = new HashMap<String, String>() {
        {
            this.put("11", "北京");
            this.put("12", "天津");
            this.put("13", "河北");
            this.put("14", "山西");
            this.put("15", "内蒙古");
            this.put("21", "辽宁");
            this.put("22", "吉林");
            this.put("23", "黑龙江");
            this.put("31", "上海");
            this.put("32", "江苏");
            this.put("33", "浙江");
            this.put("34", "安徽");
            this.put("35", "福建");
            this.put("36", "江西");
            this.put("37", "山东");
            this.put("41", "河南");
            this.put("42", "湖北");
            this.put("43", "湖南");
            this.put("44", "广东");
            this.put("45", "广西");
            this.put("46", "海南");
            this.put("50", "重庆");
            this.put("51", "四川");
            this.put("52", "贵州");
            this.put("53", "云南");
            this.put("54", "西藏");
            this.put("61", "陕西");
            this.put("62", "甘肃");
            this.put("63", "青海");
            this.put("64", "宁夏");
            this.put("65", "新疆");
            this.put("71", "台湾");
            this.put("81", "香港");
            this.put("82", "澳门");
            this.put("91", "国外");
        }
    };

    private IdCardUtil validator = null;

    /**
     * 通过构造方法初始化各个成员属性
     */
    public IdCardInfoUtil(String idcard) {
        try {
            validator = new IdCardUtil();
            if (validator.isValidatedIdcard(idcard)) {
                if (idcard.length() == 15) {
                    idcard = validator.convertIdcarBy15bit(idcard);
                }
                // 获取省份
                String provinceId = idcard.substring(0, 2);
                Set<String> key = this.cityCodeMap.keySet();
                for (String id : key) {
                    if (id.equals(provinceId)) {
                        this.province = this.cityCodeMap.get(id);
                        break;
                    }
                }

                this.provinceId = StringUtil.getInt(provinceId.concat("0000"));
                String cityId = idcard.substring(2, 4);
                this.cityId = StringUtil.getInt(provinceId.concat(cityId)
                        .concat("00"));
                String regionId = idcard.substring(4, 6);
                this.regionId = StringUtil.getInt(provinceId.concat(cityId)
                        .concat(regionId));

                // 获取性别
                String id17 = idcard.substring(16, 17);
                if (Integer.parseInt(id17) % 2 != 0) {
                    this.genderId = 1;
                    this.gender = "男";
                } else {
                    this.genderId = 2;
                    this.gender = "女";
                }

                // 获取出生日期
                String birthday = idcard.substring(6, 14);
                Date birth_date = new SimpleDateFormat("yyyyMMdd").parse(birthday);
                this.birth_day = new SimpleDateFormat("yyyy-MM-dd").format(birth_date);
                GregorianCalendar currentDay = new GregorianCalendar();
                currentDay.setTime(birth_date);
                this.year = currentDay.get(Calendar.YEAR);
                this.month = currentDay.get(Calendar.MONTH) + 1;
                this.day = currentDay.get(Calendar.DAY_OF_MONTH);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @return the province
     */
    public String getProvince() {
        return province;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @return the region
     */
    public String getRegion() {
        return region;
    }

    /**
     * @return the provinceId (6xnum)
     */
    public int getProvinceId() {
        return provinceId;
    }

    /**
     * @return the cityId (6xnum)
     */
    public int getCityId() {
        return cityId;
    }

    /**
     * @return the regionId (6xnum)
     */
    public int getRegionId() {
        return regionId;
    }

    /**
     * @return the year
     */
    public int getYear() {
        return year;
    }

    /**
     * @return the month
     */
    public int getMonth() {
        return month;
    }

    /**
     * @return the day
     */
    public int getDay() {
        return day;
    }

    /**
     * @return the gender
     */
    public int getGenderId() {
        return genderId;
    }

    /**
     * @return the genderStr
     */
    public String getGender() {
        return gender;
    }

    /**
     * @return the birth
     */
    public String getBirthDay() {
        return birth_day;
    }

    @Override
    public String toString() {
        return "省份：" + this.province + ",市：" + this.city + ",区县：" + this.region + "性别：" + this.gender + ",出生日期："
                + this.birth_day + "，provinceId:" + provinceId + ",cityId:" + cityId + ",regionId:" + regionId
                + ",genderId:" + genderId;
    }

    /**
     * 获取身份证信息
     * @return
     */
    public HashMap getInfo() {
        HashMap hashMap = new HashMap();
        hashMap.put("birth", this.birth_day);
        hashMap.put("province_id", this.provinceId);
        hashMap.put("city_id", this.cityId);
        hashMap.put("region_id", this.regionId);
        hashMap.put("gender_id", this.genderId);

        return hashMap;
    }

    public static void main(String[] args) {
        String idcard = "110101197804090080";
        IdCardInfoUtil idCardInfoUtil = new IdCardInfoUtil(idcard);
        System.out.println(idCardInfoUtil.toString());
        System.out.println(idCardInfoUtil.getInfo());
    }
}
